package com.easylinkin.linkappapi.aspect;

import com.easylinkin.linkappapi.annotation.ObjectOperationLog;
import com.easylinkin.linkappapi.asset.entity.Asset;
import com.easylinkin.linkappapi.common.service.CommonService;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import com.easylinkin.linkappapi.operatelog.entity.ObjectOperateLog;
import com.easylinkin.linkappapi.operatelog.service.ObjectOperateLogService;
import javax.annotation.Resource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class ObjectOperationLogAspect {

    private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(ObjectOperationLogAspect.class);

    @Autowired
    ObjectOperateLogService objectOperateLogService;
    @Resource
    CommonService commonService;

    @Pointcut("@annotation(com.easylinkin.linkappapi.annotation.ObjectOperationLog)")
    private void pointcut() {
    }

    @Before("pointcut() && @annotation(log)")
    public void before(JoinPoint joinPoint, ObjectOperationLog log) {
//    	 //类名
//    	 String clsName=joinPoint.getSignature().getDeclaringType().getSimpleName();
//         //方法名
//         String modName= joinPoint.getSignature().getName();
//         //参数
//         Object[] args = joinPoint.getArgs();
//         LOGGER.info(clsName);
    }

    @After("pointcut() && @annotation(log)")
    public void doAfterAdvice(JoinPoint joinPoint, ObjectOperationLog log) {

        if (LogModule.DEVICE.equals(log.module())) {
            addDeviceLog(joinPoint, log);
        } else if (LogModule.ASSET.equals(log.module())) {
            addAssetLog(joinPoint, log);
        }

    }

    private void addAssetLog(JoinPoint joinPoint, ObjectOperationLog log) {
        // 类名
        String clsName = joinPoint.getSignature().getDeclaringType().getSimpleName();
        // 方法名
        String modName = joinPoint.getSignature().getName();
        // 参数
        Object[] args = joinPoint.getArgs();
        LOGGER.info(clsName);
        if (args.length > 0) {
            for (Object obj : args) {
                if (obj instanceof Asset) {

                    Asset asset = (Asset) args[0];
                    ObjectOperateLog objectOperateLog = new ObjectOperateLog();
                    objectOperateLog.setModuleName(log.module().getName());
                    objectOperateLog.setUrl(clsName + "." + modName);
                    objectOperateLog.setOperateDesc(log.desc());

                    objectOperateLog.setObjectId(asset.getId());
                    objectOperateLog.setObjectKey(asset.getAssetName());

                    objectOperateLog.setContent(log.desc() + ",资产名称为：" + asset.getAssetName());

                    commonService.setLogUserInfo(objectOperateLog);

                    objectOperateLogService.addLog(objectOperateLog);
                }
            }
        }
    }

    public void addDeviceLog(JoinPoint joinPoint, ObjectOperationLog log) {
        // 类名
        String clsName = joinPoint.getSignature().getDeclaringType().getSimpleName();
        // 方法名
        String modName = joinPoint.getSignature().getName();
        // 参数
        Object[] args = joinPoint.getArgs();
        LOGGER.info(clsName);
        if (args.length > 0) {
            for (Object obj : args) {
                if (obj instanceof Device) {

                    Device device = (Device) args[0];
                    ObjectOperateLog objectOperateLog = new ObjectOperateLog();
                    objectOperateLog.setModuleName(log.module().getName());
                    objectOperateLog.setObjectId(device.getId());
                    objectOperateLog.setObjectKey(device.getCode());
                    objectOperateLog.setUrl(clsName + "." + modName);
                    objectOperateLog.setOperateDesc(log.desc());

                    objectOperateLog.setContent(log.desc() + ",设备编号为：" + device.getCode());

                    commonService.setLogUserInfo(objectOperateLog);

                    objectOperateLogService.addLog(objectOperateLog);
                }
            }
        }
    }


}
